package com.dajun.listener;

import liquibase.pro.packaged.R;
import org.flowable.engine.IdentityService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("nodeApproverListener")
public class NodeApproverListener implements TaskListener {
    @Resource
    private TaskService taskService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private IdentityService identityService;

    /**
     *  单节点监听类
     * @param task
     */
    @Override
    public void notify(DelegateTask task) {
        //单节点动态设置审批人

        //1 获取当前流程发起人
        String processInstanceId = task.getProcessInstanceId();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        String startUserId = processInstance.getStartUserId();
        //根据流程发起人信息获取其上级信息（如上级主管/部门经理等直属上级领导）\

        //todo 后续根据具体表字段获取其上级领导信息
        //示例，根据部门分配
        String department = (String) task.getVariable("department");
        //默认审批人
        String approver = "defaultUser";

        //根据部门获取对应审批人
        if ("IT".equals(department)) {
            approver = "techLeader";
        } else if ("HR".equals(department)) {
            approver = "hrManager";
        }
        task.setAssignee(approver);
    }
}
